<?php

namespace Database\Seeders;

use App\Models\Menu;
use App\Models\User;
use App\Models\Dish;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class MenuSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // 清空菜单表（使用delete而不是truncate避免外键约束问题）
        DB::table('menus')->delete();

        // 获取一些用户作为菜单创建者
        $users = User::take(10)->get();
        
        // 获取系统菜品
        $systemDishes = Dish::where('dish_source', 'system')->where('is_featured', true)->get();
        
        $menus = [];
        
        // 创建一个默认菜单
        $menus[] = [
            'name' => '默认菜单',
            'description' => '系统默认菜单，包含精选的经典菜品',
            'creator_id' => $users->first()->id, // 使用第一个用户作为创建者
            'source_type' => 'default',
            'source_id' => null,
            'is_public' => true,
            'is_template' => true,
            'usage_count' => 0,
            'rating' => 4.5,
            'created_at' => now()->subDays(30),
            'updated_at' => now()->subDays(0),
        ];
        
        // 创建一些用户自定义菜单
        for ($i = 1; $i <= 10; $i++) {
            $creator = $users->random();
            
            $menus[] = [
                'name' => "{$creator->nickname}的专属菜单",
                'description' => "个人定制的特色菜单",
                'creator_id' => $creator->id,
                'source_type' => 'custom',
                'source_id' => null,
                'is_public' => rand(0, 1) == 1, // 50%概率公开
                'is_template' => false,
                'usage_count' => rand(0, 20),
                'rating' => rand(30, 50) / 10, // 3.0-5.0分
                'created_at' => now()->subDays(rand(1, 30)),
                'updated_at' => now()->subDays(rand(0, 10)),
            ];
        }

        Menu::insert($menus);
        
        $this->command->info('已创建 ' . count($menus) . ' 个菜单数据');
    }
}